#include "stdafx.h"
#include "testBT.h"
#include <iostream>
using namespace std;
testBT::testBT()
{
   pBTree=NULL;
}

testBT::~testBT()
{
    
}
bool testBT::createIndex()
{

    pBTree = new BTreeDB("test.db", sizeof(KRIndexRecord), sizeof(KRIndexKey), 12);
    pBTree->open();
    pBTree->flush();
    return true;
}
bool testBT::openIndex()
{
    pBTree = new BTreeDB ("test.db");

    if (pBTree->open())
	{
	    return true;	
	}
    else
    {
        printf("failed to open the KRIndex!\n");
		return false;
    }
}
void testBT::findKeyword(string keyword)
{
    
    KRIndexKey key;
    memset(&key,0,KEYLEN);
    memcpy(&key,keyword.c_str(),keyword.size()+1);
    DbObjPtr pKey = new DbObj(key,KEYLEN);
    KRIndexRecord record;
    DbObjPtr pOldRec = new DbObj(&record,sizeof(KRIndexRecord));

    if(pBTree->get(pKey,pOldRec))
    {
        cout<<"get";
    }
	
}
bool testBT::insertKeyword(KRIndexRecord record)
{

    DbObjPtr pKey = new DbObj(&record.key,KEYLEN);
    DbObjPtr pOldRec = new DbObj(&record,sizeof(KRIndexRecord));

    if(pBTree->get(pKey,pOldRec))
    {
        for (int i=0;i<DATALEN;i++)
        {
            record.data.data[i]|=((unsigned char*)pOldRec->getData())[i];
        }
    }
    DbObjPtr pNewRec = new DbObj(&record,sizeof(KRIndexRecord));
    pBTree->put(pNewRec);
     return true;
}
